package com.alipay.sdk.abcphaima;

import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.iot.bpaas.api.service.BPaaSCallback;
import com.alipay.iot.bpaas.api.service.BPaaSResponse;
import com.alipay.iot.bpaas.api.service.LocalService;
import com.alipay.sdk.util.LogUtil;

public class AbcpQuotaAuthPage {
    public static int state = -1;

    public static void showPage(AbcpHM.AbcpScanFaceCallback callback) {
        if (!AbcpHM.getInstance().isAbcpInitSuccess()) {
            return;
        }
        state = 1;
        AbcpHM.startCanteenBPaaSService("faceVerify", "startQuotaAuth",
                new JSONObject(), new BPaaSCallback() {
                    @Override
                    public void onResponse(BPaaSResponse response) {
                        System.out.println("AbcpHM.startCanteenBPaaSService(de onResponse");
//                        if (response == null || response.getCode() == BPaaSResponse.RESULT_CODE_FAIL) {
//                            // bpaas服务调用失败
//                            callback.onFail("", "bpaas服务调用失败");
//                        } else if (response.getResult() != null) {
//                            if (response.getCode() == BPaaSResponse.RESULT_CODE_SUCCESS) {
//                                Bundle result = response.getResult().getBundle(LocalService.KEY_LOCAL_RESULT);
//                                if (result != null) {
//                                    // 处理刷脸结果
//                                    String subCode = result.getString("subCode");
//                                    String subMessage = result.getString("subMessage");
//                                    String sessionId = result.getString("sessionId");
//                                    String tradeNo = result.getString("tradeNo");
//                                    String barCode = result.getString("barCode");
//                                    String authCode, alipayUid = "";
//                                    LogUtil.d("启动刷脸 出参" + result);
//                                    if (subCode.equals("SCAN_BAR_CODE")) {
//                                        authCode = result.getString("scanCode");
//                                    } else {
//                                        alipayUid = result.getString("alipayUid");
//                                        authCode = result.getString("ftoken");
//                                    }
//                                    LogUtil.d("刷脸结果"
//                                            + "\nsessionId = " + sessionId
//                                            + "\ntradeNo = " + tradeNo
//                                            + "\nauthCode = " + authCode
//                                            + "\nbarCode = " + barCode
//                                            + "\nalipayUid = " + alipayUid);
//                                    if (TextUtils.isEmpty(authCode) || TextUtils.isEmpty(tradeNo) || TextUtils.isEmpty(sessionId)) {
//                                        callback.onFail(subCode, subMessage);
//                                    } else {
//                                        callback.onSuccess(!subCode.equals("SCAN_BAR_CODE"),
//                                                authCode, barCode, tradeNo, sessionId, alipayUid);
//                                    }
//                                }
//                            } else {
//                                String subCode = response.getSubCode();
//                                String subMessage = response.getSubMsg();
//                                LogUtil.d("启动刷脸 出参"
//                                        + "\nsubCode = " + subCode
//                                        + "\nsubMessage = " + subMessage);
//                                callback.onFail(subCode, subMessage);
//                            }
//                        }
                    }

                    @Override
                    public void onEvent(String s, String s1, Bundle bundle) {
                        JSONObject response = JSON.parseObject(s1);
                        String eventType = response.getString("messageType");
                        JSONObject content = JSON.parseObject(response.getString("messageContent"));
                        if (eventType.equals("SMILE_VERIFY_RESULT")) {
                            JSONObject result = content.getJSONObject("result");
                            String codes = result.getString("code");
                            String subMessage = result.getString("subMessage");
                            if ("1005".equals(result.getString("code")) || "1000".equals(result.getString("code"))) {
                                String subCode = result.getString("subCode");
                                String sessionId = result.getString("sessionId");
                                String tradeNo = result.getString("tradeNo");
                                String barCode = result.getString("barCode");
                                String authCode, alipayUid = "";
                                LogUtil.d("启动刷脸 出参" + result);
                                if (subCode.equals("SCAN_BAR_CODE")) {
                                    authCode = result.getString("scanCode");
                                } else {
                                    alipayUid = result.getString("alipayUid");
                                    authCode = result.getString("ftoken");
                                }

                                LogUtil.d("刷脸结果"
                                        + "\nsessionId = " + sessionId
                                        + "\ntradeNo = " + tradeNo
                                        + "\nauthCode = " + authCode
                                        + "\nbarCode = " + barCode
                                        + "\nalipayUid = " + alipayUid);
                                if (TextUtils.isEmpty(authCode) || TextUtils.isEmpty(tradeNo) || TextUtils.isEmpty(sessionId)) {
                                    callback.onFail(subCode, subMessage);
                                } else {
                                    callback.onSuccess(!subCode.equals("SCAN_BAR_CODE"),
                                            authCode, barCode, tradeNo, sessionId, alipayUid);
                                }
                            } else
                                callback.onFail(codes, subMessage);
                        }

                    }
                });


    }

    public static void close(AbcpHM.AbcpLoadingCallback callback) {
        if (!AbcpHM.getInstance().isAbcpInitSuccess()) {
            return;
        }
        AbcpHM.startCanteenBPaaSService("faceVerify", "stopQuotaAuth",
                new JSONObject(), new BPaaSCallback() {
                    @Override
                    public void onResponse(BPaaSResponse bPaaSResponse) {
                        System.out.println("响应：" + bPaaSResponse.toString());
                        state = -1;
                        callback.onSuccess("", "", "");
                    }

                    @Override
                    public void onEvent(String s, String s1, Bundle bundle) {
                        state = -1;
                        System.out.println("响应s：" + s + "s1:" + s1);
                    }
                });
    }
}
